package com.exam.dao;

import com.exam.domain.entity.Exam;
import com.exam.domain.entity.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ExamDao {
    public Exam getExamInfoById(Integer examId) {
        String sql1 = "SELECT * FROM exams WHERE exam_id = ?";
        // 执行查询操作，返回结果集
        // 执行查询操作，返回 ResultSet
        try {
            Connection connection = DBUtil.getConnection();
            ResultSet rs = DBUtil.query(connection, sql1, examId);
            if (rs.next()) {
                Exam exam = new Exam();
                exam.setExamId(rs.getInt("exam_id"));
                exam.setStatus(rs.getString("status"));
                exam.setTitle(rs.getString("title"));
                exam.setQuestionCount(rs.getInt("question_count"));
                exam.setScorePerQuestion(rs.getInt("score_per_question"));
                Integer teacherId=rs.getInt("created_by");
                exam.setCreatedBy(teacherId);
                // 再次查询，老师的名字
                String sql2 = "SELECT * FROM teacher_infos WHERE teacher_id = ?";
                // 创建一个新的连接，查询教师信息
                connection = DBUtil.getConnection();
                rs = DBUtil.query(connection, sql2,teacherId);
                if(rs.next()){
                    String teacherName = rs.getString("teacher_name");
                    exam.setTeacherName(teacherName);
                }

                // 返回查询到的用户
                return exam;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询试卷失败", e);
        }
        // 如果没有找到用户，返回 null
        return null;
    }

    public Integer activeExamStatusById(Integer examId) {
        // SQL 更新语句
        String sql = "UPDATE exams set status='active' WHERE exam_id = ?";

        try  {
            // 获取数据库连接
            Connection connection = DBUtil.getConnection();

            int result = DBUtil.update(connection, sql, examId);

            if (result > 0) {
                return 1; // 成功更新数据，返回 1
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("启动失败", e);
        }

        // 更新失败，返回 0
        return 0;
    }

    public Integer inactiveExamStatusById(Integer examId) {
        // SQL 更新语句
        String sql = "UPDATE exams set status='inactive' WHERE exam_id = ?";

        try  {
            // 获取数据库连接
            Connection connection = DBUtil.getConnection();

            int result = DBUtil.update(connection, sql, examId);

            if (result > 0) {
                return 1; // 成功更新数据，返回 1
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("停止失败", e);
        }

        // 更新失败，返回 0
        return 0;
    }

    public List<Exam> relatedExamListByQuestionId(Integer questionId) {
        String sql1 = "SELECT DISTINCT exam_id FROM exam_questions WHERE question_id = ?";
        List<Exam> examList = new ArrayList<>();

        // 使用 try-with-resources 自动管理资源
        try (
                Connection connection = DBUtil.getConnection();
                ResultSet rs = DBUtil.query(connection, sql1, questionId)
        ) {
            // 遍历每个 exam_id 查询对应的考试信息
            while (rs.next()) {
                int examId = rs.getInt("exam_id");

                // 查询 exams 表，获取试卷详细信息
                String sql2 = "SELECT * FROM exams WHERE exam_id = ?";
                try (ResultSet examRs = DBUtil.query(connection, sql2, examId)) {
                    if (examRs.next()) {
                        Exam exam = new Exam();
                        exam.setExamId(examRs.getInt("exam_id"));
                        exam.setStatus(examRs.getString("status"));
                        exam.setTitle(examRs.getString("title"));
                        exam.setQuestionCount(examRs.getInt("question_count"));
                        exam.setScorePerQuestion(examRs.getInt("score_per_question"));
                        Integer teacherId = examRs.getInt("created_by");
                        exam.setCreatedBy(teacherId);

                        // 查询教师信息
                        String sql3 = "SELECT teacher_name FROM teacher_infos WHERE teacher_id = ?";
                        try (ResultSet teacherRs = DBUtil.query(connection, sql3, teacherId)) {
                            if (teacherRs.next()) {
                                String teacherName = teacherRs.getString("teacher_name");
                                exam.setTeacherName(teacherName);
                            }
                        } // 自动关闭 teacherRs
                        // 将查询到的试卷信息加入到列表中
                        examList.add(exam);
                    }
                } // 自动关闭 examRs
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询试卷失败", e);
        }

        // 返回查询到的试卷列表
        return examList;
    }

}
